package com.zyh.demo;

public class StringDemo2 {
    public static void main(String[] args) {
        String str = "hello1JAVA2Hello3World4";
        char c = 'l';
        StringDemo2.getCount(str);
        int count = StringDemo2.getCount1(str, c);
        System.out.println(c + "在字符串中一共出现了" + count + "次");
    }

    //获取指定字符串中，大写字符，小写字母，数字的个数
    public static void getCount(String s) {
        int big = 0, small = 0, num = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c >= 'A' && c <= 'Z') {
                big++;
            } else if (c >= 'a' && c <= 'z') {
                small++;
            } else if (c >= '0' && c <= '9') {
                num++;
            }
        }
        System.out.println("该字符串中大写字母有" + big + "个\n小写字母有" + small + "个\n数字有" + num + "个");
        /*char[] chs = s.toCharArray();
        for (int i = 0; i < chs.length; i++) {
            if (chs[i] >= 'A' && chs[i] <= 'Z') {
                big++;
            } else if (chs[i] >= 'a' && chs[i] <= 'z') {
                small++;
            } else if (chs[i] >= '0' && chs[i] <= '9') {
                num++;
            }
        }*/
    }

    //判断字符串中指定某一个字符在字符串中出现的次数
    public static int getCount1(String s, char c) {
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == c) {
                count++;
            }
        }
        return count;
    }
}

